<?php

class Antz_Form_Components_OptGroup extends Antz_Form_Component
{
    protected $options = array();
    protected $value = array();

    public function __construct($title, $value=''){
        $this->title = $title;
        if(!is_array($value)) $value = array($value);
        $this->value = $value;
    }

    public function add($title, $value){
    	$selected = (in_array($value, $this->value)) ? true : false;
        if($value=='') $value = $title;
        $this->options[] = Antz::newObject('Antz_Form_Components_Option', array($title, $value, $selected));
        return $this->options[(count($this->options)-1)];
    }

    public function render(){
        $return = '<optgroup label="'.$this->title.'">';
        foreach($this->options as $k=>$v){
            $return .= $v->render();
        };
        $return .= '</optgroup>';
        return $return;
    }
}
